/**
 * 
 */
package com.netscenery.yang.model;

import org.jdom2.Element;

import com.netscenery.util.ErrTag;
import com.netscenery.util.XMLTag;
import com.netscenery.yang.YangStatement;
import com.netscenery.yang.designer.MapInfo;
import com.netscenery.yang.parser.YangParserException;

/**
 * @author llly
 *
 */
public class SimpleElement extends ModelElement {
	private String keyword;

	/* (non-Javadoc)
	 * @see com.netscenery.yang.model.ModelElement#validate()
	 */
	
	public SimpleElement(Env env,String keyword){
		if (!YangStatement.isYangKeyword(keyword)){
			return;
		}
		this.keyword = keyword;
		this.env = env;
	}
	public SimpleElement(Env env,String keyword, String name){
		this(env,keyword);
		if (null == this.keyword){
			return;
		}
		this.name = name;
	}
	@Override
	public Validator validate() {
		Validator validator = new Validator();
		if (isNullString(keyword)){
			validator.setIsValid(false);
			validator.addMsg(new Message(this, ErrTag.ERROR,"no keyword."));
		}
		Validator sValidator = super.validate();
		validator.copy(sValidator);
		return validator;
	}

	/* (non-Javadoc)
	 * @see com.netscenery.yang.model.ModelElement#toYang()
	 */
	@Override
	public YangStatement toYang() {

		
		YangStatement statement = new YangStatement(this.keyword,this.name);
		return statement;
	}

	/* (non-Javadoc)
	 * @see com.netscenery.yang.model.ModelElement#toXML()
	 */
	@Override
	public Element toXML() {
	
		Element element = null;
		if (keyword.equals(XMLTag.XML_TAG_KEY)){
			element = new Element(XMLTag.XML_TAG_KEYS);
			if (null == name){
				return null;
			}
			String[] keys = this.name.split(" ");
			int size = keys.length;
			for (int i = 0; i < size; i++){
				Element key = new Element(XMLTag.XML_TAG_KEY);
				key.setText(keys[i].trim());
				element.addContent(key);
			}
		}
		else if (keyword.equals(XMLTag.XML_TAG_UNIQUE)){
			element = new Element(XMLTag.XML_TAG_UNIQUES);
			if (null ==name){
				return null;
			}
			String[] uniques = this.name.split(" ");
			int size = uniques.length;
			for (int i = 0; i < size; i++){
				Element unique = new Element(XMLTag.XML_TAG_UNIQUE);
				unique.setText(uniques[i].trim());
				element.addContent(unique);
			}
		}
		else {
			element = new Element(this.keyword);
			if (null != this.name){
				element.setText(this.name);
			}
		}
		
		return element;
	}

	/* (non-Javadoc)
	 * @see com.netscenery.yang.model.ModelElement#isFieldMandatory(java.lang.String)
	 */
	@Override
	public boolean isFieldMandatory(String field) {
		// TODO Auto-generated method stub
		return false;
	}

	/* (non-Javadoc)
	 * @see com.netscenery.yang.model.ModelElement#readXML(org.jdom2.Element)
	 */
	@Override
	public int readXML(Element element) {
		
		return MODELELEMENT_OK;
	}

	/* (non-Javadoc)
	 * @see com.netscenery.yang.model.ModelElement#readYANG(com.netscenery.modeldesigner.util.YangStatement)
	 */
	@Override
	public int readYANG(YangStatement statement) throws YangParserException {
		
		return MODELELEMENT_OK;
	}
	public  MapInfo getMapInfo() throws SecurityException, NoSuchMethodException {
		// TODO Auto-generated method stub
		return null;
	}
	@Override
	public Element toYin() {
		// TODO Auto-generated method stub
		return null;
	}
	@Override
	public int readYin(Element element) throws ModelException {
		// TODO Auto-generated method stub
		return 0;
	}
	@Override
	public void update() {
		// TODO Auto-generated method stub
		
	}

}
